﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.BLL.AutoCutBoard;
using MINERP.MODEL.FoundationModel;

namespace MINERP.AutoCutBoard
{
    public partial class FrmSelectPrinter : Form
    {
        CutBoardBLL _bll = new CutBoardBLL();
        List<Printer_Info> printerInfoLsit;
        private int ComPort;
        public FrmSelectPrinter(int Port)
        {
            InitializeComponent();
            ComPort = Port;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                this.Hide();
                string pName = cmbxPrinterList.SelectedItem.ToString();
                Printer_Info printer = printerInfoLsit.First(p => p.PName == pName);
                ((FrmMain)this.Owner).CalibrationPrinterId = printer.PrinterID;
                string calibrationdata = _bll.CalibrationWirzard(ComPort, printer.CalibrationData == null ? "" : printer.CalibrationData);
                if (!string.IsNullOrEmpty(calibrationdata))
                {
                    printer.CalibrationData = calibrationdata;
                    _bll.SaveCalibrationData(printer);
                    MessageBox.Show("校准成功！","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    //MessageBox.Show("校准失败！");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                this.Close();
            }


        }

        private void FrmSelectPrinter_Load(object sender, EventArgs e)
        {
            try
            {
                printerInfoLsit = _bll.GetPrinterList();
                foreach (var item in printerInfoLsit)
                {
                    cmbxPrinterList.Items.Add(item.PName);
                }
                cmbxPrinterList.SelectedIndex = 0;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
